package com.fenger.sys.controller;

import com.fenger.sys.contast.SysConstast;
import com.fenger.sys.domain.LogLogin;
import com.fenger.sys.domain.User;
import com.fenger.sys.service.LogLoginService;
import com.fenger.sys.utils.ActivityUser;
import com.fenger.sys.utils.DataGirdView;
import com.fenger.sys.utils.WebUtils;
import com.fenger.sys.vo.LogLoginVo;
import com.fenger.sys.vo.UserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
public class LoginController {
    private final static String URL_LOGIN = "login/";

    @Autowired
    LogLoginService logLoginService;

    @RequestMapping(URL_LOGIN + "login")
    public String login(UserVo userVo) {
        //组装用户名 密码
        UsernamePasswordToken token = new UsernamePasswordToken(userVo.getLoginname(), userVo.getPwd());
        Subject subject = SecurityUtils.getSubject();
        HttpSession session = WebUtils.getCurrentSession();
        try {
            //认证
            subject.login(token);
            System.out.println("是否认证成功" + subject.isAuthenticated());
            ActivityUser user = (ActivityUser) subject.getPrincipal();
            session.setAttribute("user", user.getUser());
            addLogLogin();
            return "sys/index";
        } catch (UnknownAccountException e) {
            System.out.println("用户名不存在");
            session.setAttribute("msg", "用户名不存在");
        } catch (IncorrectCredentialsException e) {
            System.out.println("密码不正确");
            session.setAttribute("msg", "密码不正确");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            session.setAttribute("msg", "登录失败");
        }
        return "sys/login";
    }

    /**
     * 用于添加登录日志
     */
    private void addLogLogin() {
        LogLoginVo logLoginVo = new LogLoginVo();
        HttpSession session = WebUtils.getCurrentSession();
        User user = (User) session.getAttribute("user");
        HttpServletRequest request = WebUtils.getCurrentRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");

        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");

        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();

        }

        logLoginVo.setLoginname(user.getName() + "-" + user.getLoginname());
        logLoginVo.setLoginip(ip);
        logLoginVo.setLogintime(new Date());
        logLoginService.addLogLogin(logLoginVo);
    }
}
